package com.prp.gwt.client.widgets.user.friend;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.prp.gwt.client.user.friend.FriendListener;
import com.prp.gwt.client.user.mail.MailHistoryHandler;
import com.prp.gwt.client.user.php.PHPHistoryHandler;
import com.prp.gwt.client.vo.User;

public class RecommendItem extends Composite{

	interface Binder extends UiBinder<Widget, RecommendItem> {
	}

	private static final Binder binder = GWT.create(Binder.class);
	@UiField HTML recommender;
	@UiField Image portrait;
	@UiField HTML userName;
	@UiField HTML invite;
	@UiField HTML mail;
	@UiField HTML link;
	@UiField HTML cancel;
	
	public RecommendItem(final User user){
		initWidget(binder.createAndBindUi(this));
		recommender.setHTML("<a>"+user.getRecommender().getUsername()+"</a>向你推荐");
		recommender.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				History.newItem(PHPHistoryHandler.PREFIX_USERHOME+user.getRecommender().getId());
			}
		});
		portrait.setUrl(user.getPortrait());
		userName.setHTML("<a>"+user.getUsername()+"</a>");
		userName.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				History.newItem(PHPHistoryHandler.PREFIX_USERHOME+user.getId());
			}
		});
		invite.setHTML("<a>发送邀请</a>");
		invite.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				FriendListener.getInstance().invite(user);
				FriendListener.getInstance().deleteRecommend(user.getRecommendId());
				FriendListener.getInstance().getRecommendList();
			}
		});
		mail.setHTML("<a>发站内信</a>");
		mail.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				History.newItem(MailHistoryHandler.NEWMAIL);
			}
		});
		link.setHTML("<a>去主页</a>");
		link.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				History.newItem(PHPHistoryHandler.PREFIX_USERHOME+user.getId());
			}
		});
		cancel.setHTML("<a>忽略</a>");
		cancel.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				FriendListener.getInstance().deleteRecommend(user.getRecommendId());
				FriendListener.getInstance().getRecommendList();
			}
		});
	}
	
}
